package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AudioBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.RelaseAudioDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.db.RecorderAudioDBManager;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class RelaseAudioActivity extends BaseActivity {
    @Bind(R.id.audioIcon)
    SimpleDraweeView audioIcon;
    @Bind(R.id.titile)
    TextView titile;
    @Bind(R.id.headerIcon)
    SimpleDraweeView headerIcon;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.intruction)
    EditText intruction;

    String filePath = "";
    String musicName = "";
    String showResourceCoverImg = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relase_audio, Status.STATUS, "发布录音");
        ButterKnife.bind(this);

        filePath = getIntent().getStringExtra(RecorderActivity.FILEPATH_KEY);
        musicName = getIntent().getStringExtra(RecorderActivity.AUDIO_NAME_KEY);
        showResourceCoverImg = getIntent().getStringExtra(Constant.showResourceCoverImg);

        name.setText(LoginUserSharePrefernce.getName());
        titile.setText(musicName);
        audioIcon.setImageURI(showResourceCoverImg);
        headerIcon.setImageURI(LoginUserSharePrefernce.getHeaderPath());

    }

    @OnClick({R.id.submit_local, R.id.submit_online})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit_online:
                Map map = new HashMap();
                map.put("filePath", filePath);
                map.put("showResourceName", musicName);
                map.put("recordImg", showResourceCoverImg);
                if (!TextUtils.isEmpty(intruction.getText().toString())) {
                    map.put("intro", intruction.getText().toString());
                } else {
                    map.put("intro", "");
                }
                map.put("type", 3);
                map.put("customerId", LoginUserSharePrefernce.getCustomId());


                HttpMehtod.getInstance().upAudio(map, new CustomNetSubscriber(this) {
                    @Override
                    public void onNext(Response<ResponseBody> responseBodyResponse) {
                        //解析数据
                        BaseBean<RelaseAudioDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), RelaseAudioDataBean.class);
                        //数据业务逻辑处理
                        CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                        callBack.setCallBack(new CustomNetCallBack.NetCallBack<RelaseAudioDataBean>() {
                            @Override
                            public void success(RelaseAudioDataBean dataBean) {
                                if (!TextUtils.isEmpty(dataBean.getResult())) {
                                    ToastUtil.show("上传成功");
                                    FileUtils.deleteFile(filePath);
                                    finish();
                                }
                            }

                            @Override
                            public void fail(String errorCode) {

                            }
                        }).dealData();


                    }
                });
                break;
            case R.id.submit_local:
                isDelete = false;

                AudioBean audioBean = new AudioBean();
                audioBean.setPlayLink(filePath);
                audioBean.setCoverImg(showResourceCoverImg);
                audioBean.setName(musicName);
                audioBean.setCustomerId(Constant.TABLEID); //写死作为标识
//                audioBean.setCustomerId(LoginUserSharePrefernce.getCustomId())

                RecorderAudioDBManager.getInstance(this,Constant.SHOWRECOR_TABLENAME).addAudioBook(audioBean);
                finish();
                break;
        }
    }


    private boolean isDelete = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (isDelete) {
            FileUtils.deleteFile(filePath);
        } else {

        }
    }
}
